#include "msgserver.h"

#include "msgsocket.h"
#include "command.h"
#include "databasemagr.h"

MsgServer::MsgServer(QObject *parent) :
    AbsServer(parent)
{
}

MsgServer::~MsgServer()
{
    qDebug() << "tcp server close";
    foreach (MsgSocket *client, m_clients)
    {
        m_clients.removeOne(client);
        client->Close();
    }
}

// 有新的客户端连接进来
void MsgServer::SltNewConnection()
{
    static int uCount = 0;
    qDebug() << "MsgServer::SltNewConnection: " << ++uCount;
    MsgSocket *client = new MsgSocket(this, m_tcpServer->nextPendingConnection());
    connect(client, SIGNAL(signalConnected()),
            this, SLOT(SltConnected()));
    connect(client, SIGNAL(signalDisConnected()),
            this, SLOT(SltDisConnected()));
}

/**
 * @brief MsgServer::SltConnected
 * 通过验证后，才可以加入容器进行管理
 */
void MsgServer::SltConnected()
{
    MsgSocket *client = (MsgSocket *)this->sender();
    if (NULL == client) return;

    connect(client, SIGNAL(signalMsgToClient(quint8,int,QJsonValue)),
            this, SLOT(SltMsgToClient(quint8,int,QJsonValue)));
    connect(client, SIGNAL(signalDownloadFile(QJsonValue)),
            this, SIGNAL(signalDownloadFile(QJsonValue)));

    emit signalUserStatus(QString("用户 [%1] 上线").arg(DataBaseMagr::Instance()->GetUserName(client->GetUserId())));
    m_clients.push_back(client);
}

/**
 * @brief MsgServer::SltDisConnected
 * 有客户端下线
 */
void MsgServer::SltDisConnected()
{
    MsgSocket *client = (MsgSocket *)this->sender();
    if (NULL == client) return;

    for (int i = 0; i < m_clients.size(); i++) {
        if (client == m_clients.at(i))
        {
            m_clients.remove(i);
            Q_EMIT signalUserStatus(QString("用户 [%1] 下线").arg(DataBaseMagr::Instance()->GetUserName(client->GetUserId())));
            return;
        }
    }

    disconnect(client, SIGNAL(signalConnected()), this, SLOT(SltConnected()));
    disconnect(client, SIGNAL(signalDisConnected()), this, SLOT(SltDisConnected()));
    disconnect(client, SIGNAL(signalMsgToClient(quint8,int,QJsonValue)),
               this, SLOT(SltMsgToClient(quint8,int,QJsonValue)));
    disconnect(client, SIGNAL(signalDownloadFile(QJsonValue)),
               this, SIGNAL(signalDownloadFile(QJsonValue)));
}

/**
 * @brief MsgServer::SltMsgToClient
 * 消息转发控制
 * @param userId
 * @param msg
 */
void MsgServer::SltMsgToClient(const quint8 &type, const int &id, const QJsonValue &json)
{
    // 查找要发送过去的id
    for (int i = 0; i < m_clients.size(); i++) {
        if (id == m_clients.at(i)->GetUserId())
        {
            m_clients.at(i)->SltSendMessage(type, json);
            return;
        }
    }
}

/**
 * @brief MsgServer::SltTransFileToClient
 * @param userId
 * @param fileName
 */
void MsgServer::SltTransFileToClient(const int &userId, const QJsonValue &json)
{
    // 查找要发送过去的id
    for (int i = 0; i < m_clients.size(); i++) {
        if (userId == m_clients.at(i)->GetUserId())
        {
            m_clients.at(i)->SltSendMessage(SendFile, json);
            return;
        }
    }
}
